package com.tcm.controller.front;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.tcm.common.enums.ResponseEnum;
import com.tcm.entity.dto.CorrectExerciseDto;
import com.tcm.entity.dto.ItemCorrectDto;
import com.tcm.entity.resp.R;
import com.tcm.entity.vo.ItemCorrectResp;
import com.tcm.entity.vo.TopicRespPage;
import com.tcm.service.ItemService;
import com.tcm.utils.AccessTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 学生题目控制器
 * @author Guqier
 * @version 1.0
 * @date 2023/8/20 19:27
 */
@RestController
@RequestMapping("/stu/item")
public class StuItemController {

    @Autowired
    private ItemService itemService;

    /**
     * 根据章节获取题目
     * @param chapterId 章节ID
     * @param current   当前题目数
     * @param request   请求对象
     * @return 题目信息
     */
    @RequestMapping(value = "/by/chapter", method = RequestMethod.GET)
    public R getItemsByChapter(@RequestParam Long chapterId, @RequestParam Integer current, HttpServletRequest request) {
        TopicRespPage topicRespPage = itemService.getItemsByChapter(chapterId, current, AccessTokenUtil.getUserId(request));
        return R.success(topicRespPage);
    }

    /**
     * 根据试卷获取题目
     * @param testPaperId 试卷ID
     * @param current     当前题目数
     * @param request     请求对象
     * @return 题目信息
     */
    @RequestMapping(value = "/by/testPaper", method = RequestMethod.GET)
    public R getItemsByTestPaper(@RequestParam Long testPaperId, @RequestParam Integer current, HttpServletRequest request) {
        return R.success(itemService.getItemsByTestPaper(testPaperId, current, AccessTokenUtil.getUserId(request)));
    }

    /**
     * 根据用户获取每日一练题目
     * @param request 请求对象
     * @return 每日一练题目
     */
    @RequestMapping(value = "/by/daily", method = RequestMethod.GET)
    public R getItemsByDailyExercise(HttpServletRequest request) {
        TopicRespPage respPage = itemService.getItemsByDailyExercise(AccessTokenUtil.getUserId(request));
        if (respPage == null) {
            return R.fail(ResponseEnum.DAILY_ALREADY_FINISHED);
        }
        return R.success(respPage);
    }

    /**
     * 根据难度获取练习题目
     * @param difficultyId 难度ID
     * @param courseId     课程ID
     * @param current      当前题目数
     * @param request      请求对象
     * @return 题目信息
     */
    @RequestMapping(value = "/by/difficulty", method = RequestMethod.GET)
    public R getItemsByDifficulty(@RequestParam Long difficultyId, @RequestParam Long courseId, @RequestParam Integer current, HttpServletRequest request) {
        return R.success(itemService.getItemsByDifficulty(difficultyId, courseId, current, AccessTokenUtil.getUserId(request)));
    }

    /**
     * 随机获取练习题目
     * @param courseId 课程ID
     * @param request  请求对象
     * @return 题目信息
     */
    @RequestMapping(value = "/by/random", method = RequestMethod.GET)
    public R getItemsByRandom(@RequestParam Long courseId, HttpServletRequest request) {
        return R.success(itemService.getItemsByRandom(courseId, AccessTokenUtil.getUserId(request)));
    }

    /**
     * 根据类型获取练习题目
     * @param courseId 课程ID
     * @param type     题目类型
     * @param request  请求对象
     * @return 题目信息
     */
    @RequestMapping(value = "/by/type", method = RequestMethod.GET)
    public R getItemsByType(@RequestParam Integer current, @RequestParam Long courseId, @RequestParam Integer type, HttpServletRequest request) {
        return R.success(itemService.getItemsByType(current, courseId, type, AccessTokenUtil.getUserId(request)));
    }

    /**
     * 批量评判题目
     * @param dtos    题目答案封装类
     * @param request 请求对象
     * @return 评判结果
     */
    @RequestMapping(value = "/correct/normal", method = RequestMethod.POST)
    public R correctItemBatchByChapter(@RequestBody List<ItemCorrectDto> dtos, @RequestParam Integer type, HttpServletRequest request) {
        List<ItemCorrectResp> resps = null;
        try {
            resps = itemService.correctItemBatch(dtos, AccessTokenUtil.getUserId(request), type);
        } catch (JsonProcessingException e) {
            return R.fail(ResponseEnum.HANDLE_FAIL);
        }
        return R.success(resps);
    }

    /**
     * 评判每日一练题目
     * @param dtos    题目答案DTO
     * @param request 请求对象
     * @return 评判结果
     */
    @RequestMapping(value = "/correct/daily", method = RequestMethod.POST)
    public R correctItemBatchByDailyExercise(@RequestBody List<ItemCorrectDto> dtos, HttpServletRequest request) {
        List<ItemCorrectResp> resps = itemService.correctItemBatchByDailyExercise(dtos, AccessTokenUtil.getUserId(request));
        return resps != null ? R.success(resps) : R.fail(ResponseEnum.DAILY_ALREADY_FINISHED);
    }

    /**
     * 评判试卷练习答案
     * @param dto     答案数据
     * @param request 请求对象
     * @return 判题结果
     */
    @RequestMapping(value = "/correct/exercise", method = RequestMethod.POST)
    public R correctItemBatchByExercise(@RequestBody @Valid CorrectExerciseDto dto, HttpServletRequest request) {
        List<ItemCorrectResp> resps = null;
        try {
            resps = itemService.correctItemBatchByExercise(dto, AccessTokenUtil.getUserId(request));
        } catch (JsonProcessingException e) {
            return R.fail(ResponseEnum.HANDLE_FAIL);
        }
        return resps != null ? R.success(resps) : R.fail(ResponseEnum.TEST_PAPER_INVALID);
    }

}
